package com.qf.controller;

import com.qf.entity.PageBean;
import com.qf.pojo.Goods;
import com.qf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @RequestMapping("/getGoodsById")
    public String getGoodsById(HttpServletRequest req){
        //获取参数
        int id = Integer.parseInt(req.getParameter("id"));

        //调用service，
        Goods goods = goodsService.findById(id);

        req.setAttribute("goods",goods);
        //请求转发域
        return "forward:/goodsDetail";

    }

    @RequestMapping("/getGoodsListByTypeId")
    public String getGoodsListByTypeId(HttpServletRequest req){
        //????---字符串转换失败
        //解决---数据库字段与实体类名称不一致
        int typeId = Integer.parseInt(req.getParameter("typeId"));

        //获取每页条数
        int pageSize = 2;
        String sizeStr = req.getParameter("pageSize");
        if (sizeStr != null){
            pageSize = Integer.parseInt(sizeStr);
        }

        //获取当前页
        int pageNum = 1;
        String num = req.getParameter("pageNum");
        if (num != null){
            pageNum = Integer.parseInt(num);
        }

        //分页查询数据
        PageBean<Goods> pageBean = goodsService.findGoodsListByTypeIdPage(typeId, pageSize, pageNum);

        //把分页数据存储到请求域
        req.setAttribute("pageBean",pageBean);
        //typeId
        req.setAttribute("typeId",typeId);

        return "forward:/goodsList";

    }
}
